﻿using System;
using System.Windows.Forms;
using 售后客户管理.Common;
using 售后客户管理.ModelLibrary;

namespace 售后客户管理.Forms.InternalSetting
{
    public partial class FmColumn : Form
    {
        public FmColumn()
        {
            InitializeComponent();
        }
        private SqlConnectionHelper SqlConnectionHelper = new SqlConnectionHelper(Program.configs,ConfigCode.Main);
        private void FmColumn_Load(object sender, EventArgs e)
        {
            this.btnSave.Paint += DrawingImage.RoundButton_Paint;
            this.btnCancel.Paint += DrawingImage.RoundButton_Paint;
            this.dgvColumn.BackgroundColor = MyColor.NewColor_Lavender;
            ObjectToApp(new Column());
            this.cbbssColtype.DataSource = SqlClassLibrary.SqlGetSet.GetDictList("coltype", SqlConnectionHelper.sqlConnection);
            this.dgvColumn.DoubleBuffered(true);
            this.dgvColumn.DataSource = SqlClassLibrary.SqlGetSet.GetColumnDT("", SqlConnectionHelper.sqlConnection);
            DrawingImage.ReNameDataGridView(this.dgvColumn, SqlConnectionHelper.sqlConnection);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Column column = AppToObject();
            if (SqlClassLibrary.SqlGetSet.SetColumn(column, SqlConnectionHelper.sqlConnection) > 0)
            {
                if (MessageBox.Show("保存成功！是否清除界面信息？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                    ObjectToApp(new Column());
                this.dgvColumn.DataSource = SqlClassLibrary.SqlGetSet.GetColumnDT("", SqlConnectionHelper.sqlConnection);
                DrawingImage.ReNameDataGridView(this.dgvColumn, SqlConnectionHelper.sqlConnection);
                this.fbcColname.Focus();
                return;
            }
            MessageBox.Show("保存失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ObjectToApp(new Column());
        }
        private void dgvColumn_DoubleClick(object sender, EventArgs e)
        {
            Column column = new Column();
            column = DrawingImage.DataGridViewSelectedRowToObject(this.dgvColumn, column);
            column.Oldcolname = column.Colname;
            ObjectToApp(column);
        }

        //registry HotKeys Ctrl+S 
        private void FmColumn_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.S && e.Control)
            {
                btnSave_Click(this.btnSave, new EventArgs());
            }
        }

        private Column AppToObject()
        {
            Column column = new Column();
            column.Beizhu = this.fbcBeizhu.Text;
            column.Chnname = this.fbcChnname.Text;
            column.Colname = this.fbcColname.Text;
            column.Coltype = this.cbbssColtype.Text;
            column.Max_length = this.fbcMax_length.Text;
            column.Precise = this.fbcPrecise.Text;
            column.Oldcolname = this.tbOldcolname.Text;

            return column;
        }
        private void ObjectToApp(Column column)
        {
            this.fbcBeizhu.Text = column.Beizhu;
            this.fbcChnname.Text = column.Chnname;
            this.fbcColname.Text = column.Colname;
            this.cbbssColtype.Text = column.Coltype;
            this.fbcMax_length.Text = column.Max_length;
            this.fbcPrecise.Text = column.Precise;
            this.tbOldcolname.Text = column.Oldcolname;
        }

    }
}
